package com.wanli.storemobile.homepage.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.wanli.storemobile.R;
import com.wanli.storemobile.bean.ProductClassifyListBean;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Author: HWC
 * Description: 消息列表
 */
public class ProductClassifyListAdapter extends RecyclerView.Adapter<ProductClassifyListAdapter.ViewHolder> {

    private List<ProductClassifyListBean.DataBean.ItemListBean> listsBeans;
    private Context context;
    private OnCallBackListener onCallBackListener;

    public ProductClassifyListAdapter(List<ProductClassifyListBean.DataBean.ItemListBean> listsBeans) {
        this.listsBeans = listsBeans;
    }

    public void setOnCallBackListener(OnCallBackListener onCallBackListener) {
        this.onCallBackListener = onCallBackListener;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
        context = viewGroup.getContext();
        View inflate = LayoutInflater.from(context).inflate(R.layout.item_product_classify_list, viewGroup, false);
        ViewHolder viewHolder = new ViewHolder(inflate);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        ProductClassifyListBean.DataBean.ItemListBean dataBean = listsBeans.get(position);
        holder.tvSortNumber.setText(dataBean.getSort() + "");
        holder.tvName.setText(dataBean.getName());
        holder.tvEdit.setOnClickListener(v -> {
            onCallBackListener.onCallBack(1, position);
        });
        holder.tvDelete.setOnClickListener(v -> {
            onCallBackListener.onCallBack(2, position);
        });
    }

    @Override
    public int getItemCount() {
        return listsBeans.size() == 0 ? 0 : listsBeans.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_sort_number)
        TextView tvSortNumber;
        @BindView(R.id.tv_name)
        TextView tvName;
        @BindView(R.id.tv_edit)
        TextView tvEdit;
        @BindView(R.id.tv_delete)
        TextView tvDelete;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public interface OnCallBackListener {
        void onCallBack(int type, int position);
    }
}
